<?php

namespace App\Admin\Controllers;

use App\Models\SearchDataModel;
use App\Models\ProductNameModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class SearchController extends AdminController
{
    use HasResourceActions;
    
    protected $title = '搜索数据';
    
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.search_data'))
            ->description(trans('admin.list'))
            ->body($this->grid()->render());
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SearchDataModel());
        $grid->record_id('ID')->sortable();
        $grid->keyword('关键词');
        $grid->search_people('搜索人数')->sortable();
        $grid->search_times('搜索次数')->sortable();
        $grid->click_people('点击人数')->sortable();
        $grid->pay_rate('支付转化率')->sortable();
    
        $grid->actions(function ($actions) {
//            // 去掉删除
//            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            // 去掉查看
            $actions->disableView();
        });
        
        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->where(function ($query) {
                    $query->where('keyword', 'like', "%{$this->input}%");
                }, '关键词')->placeholder('关键词');
            });
            
//            $filter->column(1 / 2, function ($filter) {
//                $filter->equal('industry', '行业')->select(ProductNameModel::INDUSTRY);
//                $filter->equal('status', '状态')->radio([
//                                                          1 => '正常',
//                                                          0 => '禁用',
//                                                      ]);
//            });
        });
        
        return $grid;
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SearchDataModel());
        $form->display('product_id', 'ID');
        //        $form->hidden('product_name', '产品名称');
        $form->text('industry', '行业')->default('种植业')->readonly();
        //        $form->select('industry', '行业')->options(ProductNameModel::INDUSTRY)->readonly();//目前固定为种植业
        $form->select('product_name_id', '产品名称')->options(function ($id) {
            $name = ProductNameModel::find($id);
            
            if ($name) {
                return [$name->id => $name->name];
            }
        })->ajax('/admin/api/product_names')->load('variety_name', '/admin/api/variety')->help('输入名称搜索')->required();
        
        $form->text('variety_name', '产品种类')->readOnly()->help('由所选产品名称决定')->required();
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states);
        $form->saving(function (Form $form) {
            //                    $product            = ProductNameModel::find($form->product_name_id);
            //                    $form->variety_name = $product['variety_name'];
            //                    $form->product_name = $product['name'];
            $form->industry = 1;
        });
        $form->saved(function (Form $form) {
            $product = ProductNameModel::find($form->model()->product_name_id);
            
            $form->model()->variety_name = $product['variety_name'];
            $form->model()->product_name = $product['name'];
            $form->model()->save();
        });
        
        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');
        return $form;
    }
    
    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.search_data'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.search_data'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }
    
    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail($id)
    {
        $product = SearchDataModel::findOrFail($id);
        $show    = new Show($product);
        $show->panel()
            ->style('primary')
            ->title('产品详情');
        $show->product_id('产品ID');
        $show->product_name('产品名');
        $show->variety_name('品种名');
        $show->status('状态')->using(['1' => '正常', '0' => '禁用']);
        $show->created_at(trans('admin.created_at'));
        
        $show->updated_at(trans('admin.updated_at'));
        return $show;
    }
}
